/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.dqsy.muban.web;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.dqsy.muban.entity.FfTrees;
import com.jeeplus.modules.dqsy.muban.service.FfTreesService;

/**
 * 模块选择Controller
 * @author 于金洋
 * @version 2020-10-28
 */
@RestController
@RequestMapping(value = "/dqsy/muban/ffTrees")
public class FfTreesController extends BaseController {

	@Autowired
	private FfTreesService ffTreesService;

	@ModelAttribute
	public FfTrees get(@RequestParam(required=false) String id) {
		FfTrees entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ffTreesService.get(id);
		}
		if (entity == null){
			entity = new FfTrees();
		}
		return entity;
	}


	/**
	 * 模板选择树表数据
	 */
	@RequiresPermissions("dqsy:muban:ffTrees:list")
	@GetMapping("list")
	public AjaxJson list(FfTrees ffTrees) {
		return AjaxJson.success().put("list", ffTreesService.findList(ffTrees));
	}

	/**
	 * 根据Id获取模板选择数据
	 */
	@RequiresPermissions(value={"dqsy:muban:ffTrees:view","dqsy:muban:ffTrees:add","dqsy:muban:ffTrees:edit"},logical=Logical.OR)
	@GetMapping("queryById")
	public AjaxJson queryById(FfTrees ffTrees) {
		return AjaxJson.success().put("ffTrees", ffTrees);
	}

	/**
	 * 保存模板选择
	 */
	@RequiresPermissions(value={"dqsy:muban:ffTrees:add","dqsy:muban:ffTrees:edit"},logical=Logical.OR)
	@PostMapping("save")
	public AjaxJson save(FfTrees ffTrees, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(ffTrees);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑表单保存
		ffTreesService.save(ffTrees);//保存
		return AjaxJson.success("保存模板选择成功");
	}

	/**
	 * 删除模板选择
	 */
	@RequiresPermissions("dqsy:muban:ffTrees:del")
	@DeleteMapping("delete")
	public AjaxJson delete(FfTrees ffTrees) {
		ffTreesService.delete(ffTrees);
		return AjaxJson.success("删除模板选择成功");
	}

	/**
	     * 获取JSON树形数据。
	     * @param extId 排除的ID
	     * @return
	*/
	@RequiresPermissions("user")
	@GetMapping("treeData")
	public AjaxJson treeData(@RequestParam(required = false) String extId) {
		List<FfTrees> list = ffTreesService.findList(new FfTrees());
		List rootTree = getFfTreesTree(list, extId);
		return AjaxJson.success().put("treeData", rootTree);
	}

	private List<FfTrees> getFfTreesTree(List<FfTrees> list, String extId) {
		List<FfTrees> ffTreess = Lists.newArrayList();
		List<FfTrees> rootTrees = ffTreesService.getChildren("0");
		for (FfTrees root : rootTrees) {
		    if (StringUtils.isBlank(extId) ||  !extId.equals(root.getId())) {
		        ffTreess.add(getChildOfTree(root, list, extId));
		    }
		}
		return ffTreess;
	}

	private FfTrees getChildOfTree(FfTrees ffTrees, List<FfTrees> ffTreesList, String extId) {
		ffTrees.setChildren(Lists.newArrayList());
		for (FfTrees child : ffTreesList) {
		    if (StringUtils.isBlank(extId) ||  (!extId.equals(child.getId()) && child.getParentIds().indexOf("," + extId + ",") == -1)) {
		        if (child.getParentId().equals(ffTrees.getId())) {
		            ffTrees.getChildren().add(getChildOfTree(child, ffTreesList, extId));
		        }
		    }
		}
		return ffTrees;
	}



}